import * as ResourceTypeApi from "@/api/resourceType";
import * as ResourceTypeFieldApi from "@/api/resourceTypeField";
import * as EnumApi from "@/api/enum";
import * as CustomizeColumnApi from "@/api/customizeColumn";

export default {
    data() {
        const validInputNotZero = (rule, value, callback) => {
            if (String(value).trim() === "") {
                callback(new Error("请选择枚举组"));
            } else {
                callback();
            }
        };

        return {
            // 父表格数据
            resourceTypeList: [],

            // 枚举组数据列表
            enumGroupList: [],

            // 分页设置
            layout: "total, sizes, prev, pager, next, jumper",
            pageData: {
                currentPage: 1,
                pageSize: 20,
                total: 0,
            },

            // 控制添加资源类别弹窗的弹出
            addResourceTypeDialogForm: false,
            // 设置弹出框标题头
            title: "添加资源类别数据",
            // 控制资源类别信息是否可以修改
            isEditResourceTypeName: false,
            isEditResourceTypeTitle: false,
            // 添加的资源类别数据
            addResourceTypeData: {
                resourceType: {
                    resourceTypeName: "",
                    resourceTypeTitle: "",
                },
                resourceTypeFieldList: [],
            },

            // 弹出窗中label宽度
            formLabelWidth: "120px",
            // 弹窗中表格多选选中的数据
            multipleSelection: [],

            // 控制修改资源类别信息弹出框消失显示
            editResourceTypeDialogForm: false,
            // 修改资源类别信息的数据
            editResourceTypeData: {
                resourceTypeName: "",
                resourceTypeTitle: "",
            },
            // 修改资源类别信息的校验规则
            editResourceTypeDataRules: {
                resourceTypeName: [{
                    required: true,
                    message: "请完善资源类别英文名信息",
                    trigger: "blur",
                }, ],
                resourceTypeTitle: [{
                    required: true,
                    message: "请完善资源类别中文名信息",
                    trigger: "blur",
                }, ],
            },

            // 控制修改资源类别字段弹窗消失显示
            editResourceTypeFieldDialogForm: false,
            // 待修改的资源类别字段信息
            editResourceTypeFieldData: {},
            editResourceTypeFieldDataRules: {
                resourceTypeFieldName: [
                    { required: true, message: "请完善字段英文名信息", trigger: "blur" },
                ],
                resourceTypeFieldTitle: [
                    { required: true, message: "请完善字段英文名信息", trigger: "blur" },
                ],
                resourceTypeFieldType: [
                    { required: true, message: "请选择字段类型信息", trigger: "blur" },
                ],
                resourceTypeFieldRequired: [
                    { required: true, message: "请选择是否必填", trigger: "blur" },
                ],
                enumGroupName: [
                    { required: true, message: "请选择枚举组信息", trigger: "blur" },
                    { validator: validInputNotZero, trigger: "blur" },
                ],
            },
        };
    },

    methods: {
        // 获取资源类别数据列表
        getResourceTypeList() {
            ResourceTypeApi.getResourceTypeList().then((res) => {
                this.resourceTypeList = res.data;
                this.pageData.total =
                    this.resourceTypeList == null ? 0 : this.resourceTypeList.length;
            });
        },

        // 当前页发生改变
        handleCurrentChange(val) {
            this.pageData.currentPage = val;
        },
        // 当前页大小发生改变
        handleSizeChange(val) {
            this.pageData.pageSize = val;
        },
        // 格式化枚举组英文名
        formatterGroupName(row, column, cellValue, index) {
            let title = "";
            if (this.enumGroupList != null) {
                this.enumGroupList.forEach((enumGroup) => {
                    if (enumGroup.enumGroupName === cellValue) {
                        title = enumGroup.enumGroupTitle;
                    }
                });
            }
            return title;
        },

        // 格式化是否必填
        formatterRequired(row, column, cellValue, index) {
            let info = "";
            if (cellValue) {
                info = "是";
            } else {
                info = "否";
            }
            return info;
        },

        // 点击编辑按钮触发事件
        handleEditResourceType(index, row) {
            this.editResourceTypeData.resourceTypeName = row.resourceTypeName;
            this.editResourceTypeData.resourceTypeTitle = row.resourceTypeTitle;
            this.editResourceTypeDialogForm = true;
        },
        // 修改资源类别信息事件
        handleEditResourceTypeData() {
            this.$refs.editResourceTypeData.validate((valid) => {
                if (valid) {
                    ResourceTypeApi.updateResourceTypeByName(
                        this.editResourceTypeData
                    ).then((res) => {
                        if (res.code == 200) {
                            this.$message.success(res.message);
                            this.getResourceTypeList();
                            this.resetEditResourceTypeDataInput();
                            this.editResourceTypeDialogForm = false;
                        } else {
                            this.$message.error(res.message);
                        }
                    });
                }
            });
        },
        // 重置修改资源类别信息弹窗输入框
        resetEditResourceTypeDataInput() {
            this.$refs.editResourceTypeData.resetFields();
        },

        // 改变资源类别信息为不可写状态
        changeDisabledStatusTrue() {
            this.isEditResourceTypeName = true;
            this.isEditResourceTypeTitle = true;
        },

        // 改变资源类别信息为可写状态
        changeDisabledStatusFalse() {
            this.isEditResourceTypeName = false;
            this.isEditResourceTypeTitle = false;
            this.addResourceTypeData = {
                resourceType: {
                    resourceTypeName: "",
                    resourceTypeTitle: "",
                },
                resourceTypeFieldList: [],
            };
        },

        // 点击添加资源类别字段触发事件
        handleAddResourceTypeField(index, row) {
            this.addResourceTypeData.resourceType.resourceTypeName =
                row.resourceTypeName;
            this.addResourceTypeData.resourceType.resourceTypeTitle =
                row.resourceTypeTitle;
            this.changeDisabledStatusTrue();
            this.title = "添加字段数据";
            this.addResourceTypeDialogForm = true;
        },

        // 点击资源类别展开触发事件
        handleExpandRow(row, expandedRows) {
            if (!row.fields) {
                this.getResourceTypeFieldList(row.resourceTypeName);
            }
            this.$refs.resourceTypeList.toggleRowExpansion(row);
        },

        // 获取单个资源类别下资源类别字段数据
        getResourceTypeFieldList(resourceTypeName) {
            let self = this;
            ResourceTypeFieldApi.getResourceTypeFieldByName(resourceTypeName).then(
                (res) => {
                    const index = self.resourceTypeList.findIndex(
                        (data) => data.resourceTypeName === resourceTypeName
                    );
                    self.$set(self.resourceTypeList[index], "fields", res.data);
                }
            );
        },

        // 点击删除资源类别按钮触发事件
        handleDeleteResourceType(index, row) {
            ResourceTypeApi.deleteResourceTypeByName(row.resourceTypeName).then(
                (res) => {
                    this.$message.success(res.message);
                    this.getResourceTypeList();
                }
            );
        },

        // 点击删除资源类别字段按钮触发事件
        handleDeleteResourceTypeField(index, row) {
            ResourceTypeFieldApi.deleteResourceTypeFieldByFieldId(
                row.resourceTypeFieldId
            ).then((res) => {
                this.$message.success(res.message);
                this.getResourceTypeFieldList(row.resourceTypeName);

                this.updateDefaultCustomColumn(row.resourceTypeName);
            });
        },

        // 弹窗中添加资源类别字段按钮触发事件
        handleAddResourceTypeFieldData() {
            this.addResourceTypeData.resourceTypeFieldList.push({
                resourceTypeFieldName: "",
                resourceTypeFieldTitle: "",
                resourceTypeFieldUnit: "",
                resourceTypeFieldType: "",
                enumGroupName: "",
                resourceTypeName: "",
            });
        },

        // 弹窗中删除选中资源类别字段按钮触发事件
        handleDeleteResourceTypeFieldData() {
            let resourceTypeFieldList = [];
            this.addResourceTypeData.resourceTypeFieldList.forEach(
                (oldResourceTypeField) => {
                    let flag = true;
                    this.multipleSelection.forEach((deletedResourceTypeField) => {
                        if (
                            oldResourceTypeField.resourceTypeFieldName ===
                            deletedResourceTypeField.resourceTypeFieldName
                        ) {
                            flag = false;
                        }
                    });
                    if (flag) {
                        resourceTypeFieldList.push(oldResourceTypeField);
                    }
                }
            );
            this.addResourceTypeData.resourceTypeFieldList = resourceTypeFieldList;
        },

        // 设置弹出窗中动态添加资源类别字段的序号值
        rowClassName({ row, rowIndex }) {
            row.xh = rowIndex + 1;
        },

        // 弹出窗中表格多选框内容改变触发事件
        handleDetailSelectionChange(selection) {
            this.multipleSelection = selection;
        },

        // 提交添加枚举数据
        handleAddResourceTypeData() {
            this.$refs.addResourceTypeData.validate((valid) => {
                if (valid) {
                    if (!this.isEditResourceTypeName) {
                        this.addResourceType();
                    } else {
                        this.addResourceTypeField();
                    }
                } else {
                    this.$message.warning("请按要求完善信息");
                }
            });
        },
        // 重置添加枚数据弹窗输入框内容
        resetAddResourceTypeDataInput() {
            this.$refs.addResourceTypeData.resetFields();
        },

        // 添加资源类别数据
        addResourceType() {
            ResourceTypeApi.addResourceType(
                this.addResourceTypeData.resourceType
            ).then((res) => {
                if (res.code == 200) {
                    if (this.addResourceTypeData.resourceTypeFieldList.length > 0) {
                        this.addResourceTypeField();
                    }
                    this.getResourceTypeList();

                    this.updateDefaultCustomColumn(
                        this.addResourceTypeData.resourceType.resourceTypeName
                    );

                    this.$message.success(res.message);
                    this.resetAddResourceTypeDataInput();
                    this.addResourceTypeDialogForm = false;
                } else {
                    this.$message.error(res.message);
                }
            });
        },

        // 添加资源类别字段数据
        addResourceTypeField() {
            if (this.addResourceTypeData.resourceTypeFieldList.length > 0) {
                let requestDataList = this.addResourceTypeData;
                requestDataList.resourceTypeFieldList.forEach((field) => {
                    field.resourceTypeName = this.addResourceTypeData.resourceType.resourceTypeName;
                });
                ResourceTypeFieldApi.addResourceTypeFieldList(requestDataList).then(
                    (res) => {
                        if (res.code == 200) {
                            this.getResourceTypeList();
                            this.$message.success(res.message);
                            this.resetAddResourceTypeDataInput();
                            this.addResourceTypeDialogForm = false;
                            this.title = "添加资源类别数据";

                            this.updateDefaultCustomColumn(
                                this.addResourceTypeData.resourceType.resourceTypeName
                            );
                        } else {
                            this.$message.error(res.message);
                            res.data.forEach((element) => {
                                this.$message.error(element);
                            });
                        }
                    }
                );

                let needToDealNumber = this.addResourceTypeData.resourceTypeFieldList
                    .length;
                let count = 0;
                let successNumber = 0;
                // this.addResourceTypeData.resourceTypeFieldList.forEach(
                //   (resourceTypeField) => {
                //     resourceTypeField.resourceTypeName = this.addResourceTypeData.resourceType.resourceTypeName;
                //     ResourceTypeFieldApi.addResourceTypeField(resourceTypeField).then(
                //       (res) => {
                //         count++;
                //         if (res.code == 200) {
                //           successNumber++;
                //         }
                //         if (needToDealNumber == count) {
                //           this.getResourceTypeList();
                //           this.$message.success("添加成功字段:" + successNumber + "个");
                //           this.resetAddResourceTypeDataInput();
                //           this.addResourceTypeDialogForm = false;
                //           this.title = "添加资源类别数据";
                //         }
                //       }
                //     );
                //   }
                // );
            } else {
                this.$message.warning("请添加一个字段信息");
            }
        },

        // 修改字段信息
        handleEditResourceTypeField(index, row) {
            this.editResourceTypeFieldDialogForm = true;
            this.editResourceTypeFieldData = row;
            if (row.resourceTypeFieldRequired) {
                this.editResourceTypeFieldData.resourceTypeFieldRequired = "true";
            } else {
                this.editResourceTypeFieldData.resourceTypeFieldRequired = "false";
            }
        },
        // 修改字段信息事件
        handleEditResourceTypeFieldData() {
            this.$refs.editResourceTypeFieldData.validate((valid) => {
                if (valid) {
                    if (this.editResourceTypeFieldData.resourceTypeFieldType != "enum") {
                        this.$set(this.editResourceTypeFieldData, "enumGroupName", "");
                    }
                    ResourceTypeFieldApi.updateResourceTyprFieldByFieldId(
                        this.editResourceTypeFieldData
                    ).then((res) => {
                        if (res.code == 200) {
                            this.$message.success(res.message);
                            this.getResourceTypeFieldList(
                                this.editResourceTypeFieldData.resourceTypeName
                            );
                            this.resetEditResourceTypeFieldDataInput();
                            this.editResourceTypeFieldDialogForm = false;

                            this.updateDefaultCustomColumn(
                                this.editResourceTypeFieldData.resourceTypeName
                            );
                        } else {
                            this.$message.error(res.message);
                        }
                    });
                } else {
                    this.$message.warning("请完善必填信息");
                }
            });
        },
        // 重置修改字段信息数据
        resetEditResourceTypeFieldDataInput() {
            this.$refs.editResourceTypeFieldData.resetFields();
        },

        // 获取枚举组列表
        getEnumGroupList() {
            EnumApi.getEnumGroupList().then((res) => {
                this.enumGroupList = res.data;
            });
        },

        // 更新某一资源类别默认显示列
        updateDefaultCustomColumn(resourceTypeName) {
            CustomizeColumnApi.generateDefaultColumn(
                resourceTypeName
            ).then((res) => {});
        },
    },

    mounted() {
        this.getResourceTypeList();
        this.getEnumGroupList();
    },
};